package bb.lanxing.lib.devices.bici.cmd;

//import com.umeng.analytics.pro.cc;
import bb.lanxing.lib.devices.bici.BLECommand;
import bb.lanxing.lib.devices.bici.BiciDate;
import bb.lanxing.lib.devices.bici.model.CyclingData;
import bb.lanxing.lib.devices.bici.model.GPSData;
import java.io.Serializable;
import java.nio.ByteBuffer;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

public class CyclingCMD extends BLECommand implements Serializable {
    public static final byte COMMAND_ID = 5;
    public static final short CYCLING_LEN = 20;
    public static final int FLAG_SPORT_STATUS_PAUSE = 3;
    public static final int FLAG_SPORT_STATUS_RIDING = 1;
    public static final int FLAG_SPORT_STATUS_START = 0;
    public static final int FLAG_SPORT_STATUS_STOP = 2;
    public static final byte KEY_CYCLING = 1;
    public static final byte KEY_FLAG = 4;
    public static final byte KEY_HEARTRATE = 3;
    public static final byte KEY_LOCATION = 2;
    public static final short LOCATION_LEN = 20;
    private int alt;
    private short crank;
    private int cumulativecrank;
    private int cumulativewheel;
    private CyclingData cyclingData;
    private byte[] cyclingDatas;
    private int distance;
    private int duration;
    private byte flag;
    private final DateFormat format;
    private GPSData gpsData;
    private byte heartrate;
    private int lat;
    private byte[] locationDatas;
    private int lon;
    private int speed;
    private long timestamp;
    private short wheel;

    public CyclingCMD() {
        this.format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        this.commandId = (byte) 5;
    }

    public CyclingCMD(byte[] bArr) {
        super(bArr);
        this.format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    }

    @Override
    public void parseBody(byte[] bArr) {
        if (bArr.length < 3) {
            return;
        }
        ByteBuffer wrap = ByteBuffer.wrap(bArr);
        byte b = wrap.get();
        int length = (bArr.length - wrap.getShort()) - 3;
        byte[] bArr2 = new byte[length];
        if (b == 1) {
            byte[] bArr3 = new byte[20];
            this.cyclingDatas = bArr3;
            wrap.get(bArr3);
            parseCyclingDatas();
        } else if (b == 2) {
            byte[] bArr4 = new byte[20];
            this.locationDatas = bArr4;
            wrap.get(bArr4);
            parseLocationDatas();
        } else if (b == 3) {
            this.heartrate = wrap.get();
        } else if (b == 4) {
            this.flag = wrap.get();
        }
        if (wrap.remaining() < length) {
            return;
        }
        wrap.get(bArr2);
        parseBody(bArr2);
    }

    private void parseCyclingDatas() {
        ByteBuffer wrap = ByteBuffer.wrap(this.cyclingDatas);
        this.cumulativewheel = wrap.getInt();
        this.cumulativecrank = wrap.getInt();
        this.crank = wrap.getShort();
        this.wheel = wrap.getShort();
        this.duration = wrap.getInt();
        this.distance = wrap.getInt();
        CyclingData cyclingData = new CyclingData();
        cyclingData.setCrank(this.crank);
        cyclingData.setWheel(this.wheel);
        cyclingData.setCumulativecrank(this.cumulativecrank);
        cyclingData.setCumulativewheel(this.cumulativewheel);
        setCyclingData(cyclingData);
    }

    private void parseLocationDatas() {
        ByteBuffer wrap = ByteBuffer.wrap(this.locationDatas);
        this.lon = wrap.getInt();
        this.lat = wrap.getInt();
        this.alt = wrap.getInt();
        this.timestamp = BiciDate.biciDateToTimestamp(wrap.getInt());
        this.speed = wrap.getInt();
        GPSData gPSData = new GPSData();
        gPSData.setLon(this.lon);
        gPSData.setLat(this.lat);
        gPSData.setAlt(this.alt);
        gPSData.setTimestamp(System.currentTimeMillis());
        gPSData.setSpeed(this.speed);
        setGpsData(gPSData);
    }

    @Override
    protected byte[] encodeCMDBody(Map<Byte, Object> map) {
        int i;
        ByteBuffer allocate = ByteBuffer.allocate(500);
        allocate.put((byte) 5);
        allocate.put((byte) 16);//cc.n
        if (map.get((byte) 1) != null) {
            allocate.put((byte) 1);
            allocate.putShort((short) 20);
            allocate.put((byte[]) map.get((byte) 1));
            i = 25;
        } else {
            i = 2;
        }
        if (map.get((byte) 2) != null) {
            allocate.put((byte) 2);
            allocate.putShort((short) 20);
            allocate.put((byte[]) map.get((byte) 2));
            i += 23;
        }
        if (map.get((byte) 3) != null) {
            allocate.put((byte) 3);
            allocate.putShort((short) 1);
            allocate.put(((Byte) map.get((byte) 3)).byteValue());
            i += 4;
        }
        if (map.get((byte) 4) != null) {
            allocate.put((byte) 4);
            allocate.putShort((short) 1);
            allocate.put(((Byte) map.get((byte) 4)).byteValue());
            i += 4;
        }
        byte[] bArr = new byte[i];
        allocate.flip();
        allocate.get(bArr);
        return bArr;
    }

    public String toString() {
        return "lon = " + this.lon + "\n lat = " + this.lat + "\n timestamp = " + this.timestamp + "\n timeString = " + this.format.format(new Date(this.timestamp)) + "\n heartrate = " + ((int) this.heartrate) + "\n alt = " + this.alt + "\n speed = " + this.speed + "\n cum_crank = " + this.cumulativecrank + "\n cum_wheel = " + this.cumulativewheel + "\n crank = " + ((int) this.crank) + "\n wheel = " + ((int) this.wheel) + "\n duration = " + this.duration + "\n distance = " + this.distance + "\n flag = " + ((int) this.flag);
    }

    public GPSData getGpsData() {
        return this.gpsData;
    }

    public void setGpsData(GPSData gPSData) {
        this.gpsData = gPSData;
    }

    public byte getHeartrate() {
        return this.heartrate;
    }

    public void setHeartrate(byte b) {
        this.heartrate = b;
    }

    public byte getFlag() {
        return this.flag;
    }

    public void setFlag(byte b) {
        this.flag = b;
    }

    public CyclingData getCyclingData() {
        return this.cyclingData;
    }

    public void setCyclingData(CyclingData cyclingData) {
        this.cyclingData = cyclingData;
    }

    public int getLon() {
        return this.lon;
    }

    public void setLon(int i) {
        this.lon = i;
    }

    public int getLat() {
        return this.lat;
    }

    public void setLat(int i) {
        this.lat = i;
    }

    public int getAlt() {
        return this.alt;
    }

    public void setAlt(int i) {
        this.alt = i;
    }

    public long getTimestamp() {
        return this.timestamp;
    }

    public void setTimestamp(long j) {
        this.timestamp = j;
    }

    public int getSpeed() {
        return this.speed;
    }

    public void setSpeed(int i) {
        this.speed = i;
    }

    public short getCrank() {
        return this.crank;
    }

    public void setCrank(short s) {
        this.crank = s;
    }

    public short getWheel() {
        return this.wheel;
    }

    public void setWheel(short s) {
        this.wheel = s;
    }

    public int getCumulativecrank() {
        return this.cumulativecrank;
    }

    public void setCumulativecrank(int i) {
        this.cumulativecrank = i;
    }

    public int getCumulativewheel() {
        return this.cumulativewheel;
    }

    public void setCumulativewheel(int i) {
        this.cumulativewheel = i;
    }

    public int getDuration() {
        return this.duration;
    }

    public void setDuration(int i) {
        this.duration = i;
    }

    public int getDistance() {
        return this.distance;
    }

    public void setDistance(int i) {
        this.distance = i;
    }

    public byte[] getCyclingDatas() {
        return this.cyclingDatas;
    }

    public void setCyclingDatas(byte[] bArr) {
        this.cyclingDatas = bArr;
    }

    public byte[] getLocationDatas() {
        return this.locationDatas;
    }

    public void setLocationDatas(byte[] bArr) {
        this.locationDatas = bArr;
    }

    public static int parseFlag(byte[] bArr, int i) {
        if (bArr == null || i >= bArr.length) {
            return -1;
        }
        int i2 = i + 1;
        byte b = bArr[i];
        int i3 = i2 + 1;
        int i4 = i3 + 1;
        short s = (short) (((bArr[i2] << 8) | bArr[i3]) & 65535);
        if (b == 4) {
            return bArr[i4];
        }
        return parseFlag(bArr, i4 + s);
    }
}
